//
//  RBNoticeDetailVC.m
//  zhongzhenglu
//
//  Created by mac on 2022/12/15.
//

#import "RBNoticeDetailVC.h"
#import "UIView+RBAdd.h"
#import <UIView+YYAdd.h>
#import "UIScrollView+RBAdd.h"
#import "RanTool.h"
#import <Masonry.h>
#import "RBDefine.h"
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import "RBSystemDetailGuessCell.h"
#import "RBSystemDetailGuessHeader.h"
#import "RBCollectionReusableView.h"
#import "RBGoodsDetailWebView.h"
#import "UIViewController+RBAdd.h"
#import "RBNoticeDetailResponse.h"
#import "RBGoodsDetailVC.h" //详情

@interface RBNoticeDetailVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong)UIView *detailView;
@property(nonatomic, strong) RBGoodsDetailWebView *detailWebView;
@property(nonatomic, strong) RBGroupModel *detailWebGroup;

@property(nonatomic, strong) RBNoticeDetailResponse *response;

@end

@implementation RBNoticeDetailVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"公告详情";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
}

- (void)fillData
{
    __weak typeof(self) weak_self = self;
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.headerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
        group.headerView = UIView.new;
        group.headerReferenceSize = CGSizeMake(kRBMainScreenWidth, Zoom(10));
        group;
    })];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        self.detailWebGroup = group;
        group.headerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
        group.headerView = self.detailView;
        group.headerReferenceSize = CGSizeMake(kRBMainScreenWidth, self.detailView.esSize.height);
        group;
    })];
    
    self.detailWebView.content = self.response.data.content;
    
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.headerReuseIdentifier = NSStringFromClass(RBCollectionReusableView.class);
        group.headerReferenceSize = CGSizeMake(kRBMainScreenWidth, Zoom(60));
        group.headerView = RBSystemDetailGuessHeader.new;
        group.sectionInset = UIEdgeInsetsMake(0, Zoom(15), Zoom(15), Zoom(15));
        group.minimumLineSpacing = Zoom(11);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            for (NSInteger i = 0; i < self.response.data.goods.count; i++)
            {
                RBNoticeDetailGoodsModel *model = self.response.data.goods[i];
                [arrayM addObject:({
                    RBCellModel *cellModel = RBCellModel.new;
                    cellModel.reuseCellIdentifier = NSStringFromClass(RBSystemDetailGuessCell.class);
                    cellModel.cellData = model;
                    cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                        RBGoodsDetailVC *vc = RBGoodsDetailVC.new;
                        vc.id = model.goods_id;
                        [self.navigationController pushViewController:vc animated:YES];
                    };
                    cellModel;
                })];
            }
            arrayM;
        });
        group;
    })];
    [self.collectionView reloadData];
}

#pragma mark - Network

- (void)getData
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"gonggao_id"] = self.id;
    } path:@"han_index_gonggao_detail" post:YES log:YES responseClass:RBNoticeDetailResponse.class desView:self.view showHud:YES showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:nil reloadBlock:^{
        [weak_self getData];
    }];
}

#pragma mark - UICollcetionViewDataSource & Delegate

kRBCollectionViewDataSourceAndDelegateAuto(self.dataSource)

#pragma mark - Setter

#pragma mark - Getter

- (RBGoodsDetailWebView *)detailWebView
{
    if (!_detailWebView)
    {
        self.detailWebView = ({
            RBGoodsDetailWebView *view = [RBGoodsDetailWebView.alloc initWithFrame:CGRectMake(0, 0, kRBMainScreenWidth - Zoom(12)*2 - Zoom(17)*2, 10)];
            view;
        });
    }
    return _detailWebView;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
            layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;
            UICollectionView *view = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = UIColor.clearColor;
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(0);
            }];
            view;
        });
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = NSMutableArray.array;
    }
    return _dataSource;
}

- (UIView *)detailView
{
    if (!_detailView)
    {
        self.detailView = ({
            UIView *view = UIView.new;
            __weak typeof(self) weak_self = self;
            
            UIView *containerView = UIView.new;
            containerView.backgroundColor = UIColor.whiteColor;
            [containerView rbSetCornerRadius:Zoom(5)];
            [view addSubview:containerView];
            [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.mas_equalTo(0);
                make.left.mas_equalTo(Zoom(12));
                make.right.mas_offset(-Zoom(12));
                make.width.mas_equalTo(kRBMainScreenWidth - Zoom(12)*2);
            }];
        
            UIView *titleL = ({
                UILabel *view = UILabel.new;
                view.numberOfLines = 0;
                view.textColor = [RBThemeConfig textBlackColor];
                view.font = [UIFont boldSystemFontOfSize:Zoom(18)];
                view.text = self.response.data.title;
                view;
            });
            [containerView view_addView:titleL withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(25) bottom:0 left:Zoom(17) right:Zoom(17) centerX:0];
            
            UIView *timeL = ({
                UILabel *view = UILabel.new;
                view.numberOfLines = 0;
                view.textColor = [RBThemeConfig textGrayColor];
                view.font = [UIFont systemFontOfSize:Zoom(13)];
                view.text = self.response.data.create_time;
                view;
            });
            [containerView view_addView:timeL withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(14) bottom:0 left:Zoom(17) right:Zoom(17) centerX:0];
            
            [containerView view_addView:self.detailWebView withHeight:10 width:CGFLOAT_MIN top:Zoom(19) bottom:Zoom(29) left:Zoom(17) right:Zoom(17) centerX:0];
            
            self.detailWebView.heightChangeBlock = ^(CGFloat currentHeight) {
                [weak_self.detailWebView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.height.mas_equalTo(currentHeight);
                }];
                weak_self.detailWebGroup.headerReferenceSize = CGSizeMake(kRBMainScreenWidth, weak_self.detailWebGroup.headerView.esSize.height);
                if(weak_self.detailWebGroup)
                {
                    NSInteger section = [weak_self.dataSource indexOfObject:weak_self.detailWebGroup];
                    [weak_self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:section]];
                }
            };
            
            view;
        });
    }
    return _detailView;
}

@end
